package test19.homework;

import org.testng.annotations.Test;

import java.io.*;
import java.util.Properties;

public class Homework03 {
    public static void main(String[] args) throws IOException {
//            (1) 要编写一个dog.properties   name=tom age=5 color=red
//            (2) 编写Dog 类(name,age,color)  创建一个dog对象，读取dog.properties 用相应的内容完成属性初始化, 并输出
//            (3) 将创建的Dog 对象 ，序列化到 文件 e:\\dog.dat 文件
        String filePath = "src\\test19\\homework\\dog.properties";
        Properties properties = new Properties();
        properties.load(new FileReader(filePath));
        String name = properties.getProperty("name");
        int age = Integer.parseInt(properties.getProperty("age"));
        String color = properties.getProperty("color");

        Dog dog = new Dog(name, age, color);
        System.out.println("===dog对象信息====");
        System.out.println(dog);

        String filePath2 = "src\\test19\\homework\\dog2.properties";
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(filePath2));
        objectOutputStream.writeObject(dog);
        objectOutputStream.close();
    }
    @Test
    public void m1() throws IOException, ClassNotFoundException {
        String filePath2 = "src\\test19\\homework\\dog2.properties";
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(filePath2));
        Object dog = objectInputStream.readObject();
        Dog dog1 = (Dog)dog;
        System.out.println(dog1);
        objectInputStream.close();
    }
}

class Dog implements Serializable {
    private String name;
    private int age;
    private String color;

    public Dog(String name, int age, String color) {
        this.name = name;
        this.age = age;
        this.color = color;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", color='" + color + '\'' +
                '}';
    }
}